# 获取糗事百科第一页数据
from selenium import webdriver


class QiuBaiSpider(object):

    def __init__(self):
        self.url = "https://www.qiushibaike.com/text/page/1/"
        self.driver = webdriver.Chrome("./chromedriver")

    def parse_data(self):
        """
        解析数据的方法
        :return:
        """
        # 对数据进行分组
        div_list = self.driver.find_elements_by_xpath("//div[@class='col1 old-style-col1']/div")
        content_list = []
        for div in div_list:
            item = {}
            item["username"] = div.find_element_by_xpath(".//h2").text
            item["content"] = div.find_element_by_xpath(".//div[@class='content']/span").text
            print(item)

            content_list.append(item)
        return content_list

    def save_data(self, content_list):
        """
        数据保存的方法
        :param content_list:
        :return:
        """
        for content in content_list:
            print(content)

    def run(self):
        # 1. 请求第一页的url地址
        self.driver.get(self.url)
        # 解析第一页的数据
        content_list = self.parse_data()
        # 保存数据的方法
        self.save_data(content_list)

        while True:
            # 未找到返回空列表  find_elements  不加s 未找到抛出异常
            next_url = self.driver.find_elements_by_xpath("//span[@class='next']")
            if len(next_url) > 0:
                next_url[0].click()
                content_list = self.parse_data()
                self.save_data(content_list)
            else:
                break


if __name__ == '__main__':
    qiubai = QiuBaiSpider()
    qiubai.run()
